﻿layui.define(['form'], function () {
    var $ = layui.jquery;
    var form = layui.form;
    let layer = layui.layer;

    //自定义验证规则
    form.verify({
        account: [reg.account, '账号长度为4~20个字符，由字母数据以及@@_- 符号组成'],
        password: [reg.pwd, '密码必须6到20位，且不能出现空格'],
        verifyCode: [/^[A-Za-z0-9]{4}$/, "验证码输入错误"]
    });

    //记录用户有没有点击过登录按钮,如果点击过登录按钮,那么当滑块验证码通过后直接登录
    let isClickLogin = false;
    form.on('submit(submit)', function (data) {


        //判断是否通过了滑块验证码
        if (!isCaptchaValidate) {
            //记录用户已经点击了登录,一会滑块验证码通过后直接登录进入系统
            isClickLogin = true;
            //打开滑块验证码
            $("#openCaptcha").click();
            return false;
        }



        var returnurl = $("#returnurl").val();
        //if (returnurl == undefined || returnurl == null || returnurl == '') {
        //   returnurl = '';
        //}
        data.field.remember = data.field.remember === 'on' ? true : false;
        var encryptor = new JSEncrypt()  // 创建加密对象实例
        encryptor.setPublicKey($("#sldl").val())//设置公钥
        var rsaPassWord = encryptor.encrypt(data.field.Password)  // 对内容进行加密
        data.field.Password = rsaPassWord;
        abc.post({
            isAutoEnableBtn: false
            , url: $("#loginurl").val()
            , data: data.field
            , success: function (result) {
                abc.tipMsg({ succeeded: result.Succeeded, errors: result.Errors, successMsg: "登录成功！", failMsg: "登录失败！" });
                if (result.Succeeded == true) {
                    setTimeout(function () {
                        location = returnurl;
                    }, 1500);
                }
                $(".imgVerifyCode").click();
            }
            , fail: function (result) {
                abc.tipMsg({ succeeded: result.Succeeded, errors: result.Errors, failMsg: "登录失败" });
                abc.EnableSubmitBtn()
                $(".imgVerifyCode").click();
            }
        });

        //    abc.login({
        //        url: $("#loginurl").val(), data: data.field, returnurl: returnurl, logindone: function (result) {
        //            abc.tipMsg({ succeeded: result.Succeeded, errors: result.Errors, successMsg: "登陆成功！", failMsg: "登陆失败！" });
        //            if (result.Succeeded == false) {
        //                $("#imgVerifyCode").click();
        //            }
        //        }
        //    });
        return false;
    });

    //打开滑块验证码的层级,用于验证通过后自动关闭
    let CaptchaLayer = -1;
    //是否验证过滑块验证码
    let isCaptchaValidate = false;
    $("#openCaptcha").click(function () {

        if (isCaptchaValidate) {
            layer.msg("您已经通过验证啦")
        } else {
            CaptchaLayer = layer.open({
                title: '进行人机验证',
                type: 2,
                content: $("#captchaUrl").val(),
                area: ['400px', '430px'],
                maxmin: false, // 允许全屏最小化
                closeBtn: 1,
                resize: false,
            });
        }
    });

    //设置滑块验证码的id
    SetCaptchaId = function (id) {
        $("#captchaId").val(id);
    }
    //滑块验证码验证成果
    CaptchaValidateSuccess = function () {


        //关闭滑动验证码层
        layer.close(CaptchaLayer);

        //改变按钮文字
        $("#openCaptcha").text("已通过人机验证")
        //改变按钮样式
        $("#openCaptcha").removeClass("layui-bg-blue");
        $("#openCaptcha").addClass("layui-btn-captcha-success");

        isCaptchaValidate = true;



        //点击登录
        if (isClickLogin) {
            $("#btnlogin").click();
        }
    }
    $(".imgVerifyCode").click(function () {
        $(this).attr("src", $("#verurl").val() + "?r=" + Math.random());
    });
    $(".inputlogin").keypress(function (e) {
        if (e.which == 13) {
            $("#btnlogin").click();
        }
    });
})